package com.example.s.Activity;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.s.R;
import com.example.s.utils.LiveDataBus;
import com.example.s.utils.MyUtils;
import com.example.s.utils.PermissionsUtils;
import com.example.s.view.MyWebView;

public class UserHeadPhotoDetail extends BaseActivity {
    private WebView wv;
    private ImageView back;
    private TextView savePic;

    private int resId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_userheadphoto_detail);
        initView();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        LiveDataBus.getInstance().with("currentHeadPhoto", Integer.class).observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                resId = integer;
                new MyWebView(wv).setDrawableSrc(UserHeadPhotoDetail.this, integer);
            }
        });

        savePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (resId == 0)
                    return;
                String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
                PermissionsUtils.getInstance().chekPermissions(UserHeadPhotoDetail.this, permissions, permissionsResult);
            }
        });
    }

    PermissionsUtils.IPermissionsResult permissionsResult = new PermissionsUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
            Log.d(TAG, "passPermissons: 进入");
            Glide.with(UserHeadPhotoDetail.this)
                    .asBitmap()
                    .load(resId)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            Log.d(TAG, "onResourceReady: ");
                            MyUtils.getInstance().saveImage(UserHeadPhotoDetail.this,resource);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {

                        }
                    });
        }

        @Override
        public void forbitPermissons() {
            Toast.makeText(UserHeadPhotoDetail.this, "没有相册访问权限", Toast.LENGTH_SHORT).show();
        }
    };



    private void initView() {
        wv = (WebView) findViewById(R.id.wv);
        back = (ImageView) findViewById(R.id.back);
        savePic = (TextView) findViewById(R.id.savePic);
    }
}
